package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.ModuleFeatureBeanRowWapper;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.DataCategory;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.wapper.NodeInfoRowWapper;

/**
 * 适配器接口，用于查询交换节点列表、适配器列表和系统列表
 * 
 * @日期：Nov 11, 2014 4:15:55 PM
 * @作者：蒋佩钊
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class AdapterDao extends BaseDao {
	
	
	
	/**
	 * 通过系统id返回与其相关的数据类型列表
	 * @param sysIdArray 系统id矩阵
	 * @return
	 * @throws DaoException
	 */
	public List<DataCategory> getDataCatListBySysArray(String[] sysIdArray) throws DaoException {
		WhereCondition whereCondition = new WhereCondition();
		whereCondition.where().in("t1.SYS_ID", sysIdArray);
		
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.DATA_CODE,                          ");
		sql.append("        t1.DATA_NAME,                          ");
		sql.append("        t3.DATA_CODE PCODE,                    ");
		sql.append("        t2.SYS_CODE,                           ");
		sql.append("        t1.SELF_DESC,                          ");
		sql.append("        t2.SYS_NAME,                           ");
		sql.append("        t1.MTIME,                              ");
		sql.append("        t1.REMARK                              ");
		sql.append(" FROM                                          ");
		sql.append(         SI_TABLE("turbo_data_category") + " t1 ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("turbo_sys_info") +      " t2 ");
		sql.append(" ON                                            ");
		sql.append("        t2.id = t1.sys_id                      ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("turbo_data_category") + " t3 ");
		sql.append(" ON                                            ");
		sql.append("        t3.ID = t1.PID                         ");
		
		try {
			return CM.getDao().list("通过系统id返回与其相关的数据类型列表", sql.toString(), new ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class), whereCondition, new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("通过系统id返回与其相关的数据类型列表失败！", e);
		}
	}
	
	/**
	 * 获取某系统ID下的所有数据分类列表
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<DataCategory> getDataCatListBySysId(String sysId) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.DATA_CODE,                          ");
		sql.append("        t1.DATA_NAME,                          ");
		sql.append("        t3.DATA_CODE PCODE,                    ");
		sql.append("        t2.SYS_CODE,                           ");
		sql.append("        t1.SELF_DESC,                          ");
		sql.append("        t2.SYS_NAME,                           ");
		sql.append("        t1.MTIME,                              ");
		sql.append("        t1.REMARK                              ");
		sql.append(" FROM                                          ");
		sql.append(         SI_TABLE("turbo_data_category") + " t1 ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("turbo_sys_info") +      " t2 ");
		sql.append(" ON                                            ");
		sql.append("        t2.id = t1.sys_id                      ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("turbo_data_category") + " t3 ");
		sql.append(" ON                                            ");
		sql.append("        t3.ID = t1.PID                         ");
		sql.append(" WHERE                                         ");
		sql.append("        t1.SYS_ID = ?                          ");

		try {
			return CM.getDao().list("获取某系统ID下的所有数据分类列表", sql.toString(), new  ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class), new Object[] {sysId});
		} catch (DaoException e) {
			throw new DaoException("获取某系统ID下的所有数据分类列表失败！", e);
		}
	}
	
	/**
	 * 根据节点编码获取节点id
	 * @param nodeCode
	 * @return
	 * @throws DaoException
	 */
	public String getNodeId(String nodeCode) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.ID                             ");
		sql.append(" FROM                                     ");
		sql.append(         SI_TABLE("turbo_node_info") + " t1 ");
		sql.append(" WHERE                                    ");
		sql.append("        t1.NODE_CODE = ?                   ");
		
		try {
			return CM.getDao().queryForString("通过节点编码返回节点ID", sql.toString(), new Object[] {nodeCode});
		} catch (DaoException e) {
			throw new DaoException("通过节点编码返回节点ID失败！", e);
		}
	}
	
	/**
	 * 通过节点编码获取交换关系节点列表
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<NodeInfo> getNodeList(String nodeId) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT DISTINCT t2.NODE_CODE,             ");
		sql.append("                 t4.NODE_CODE PCODE,       ");
		sql.append("                 t2.NODE_NAME,             ");
		sql.append("                 t2.ISLOCAL,               ");
		sql.append("                 t2.STATUS,                ");
		sql.append("                 t2.REMARK,                ");
		sql.append("                 t2.INSTALL_URL,           ");
		sql.append("                 t2.ID,                    ");
		sql.append("                 t2.ADMINEMAIL,            ");
		sql.append("                 t2.HAS_GAP,               ");
		sql.append("                 t2.NET_TYPE,              ");
		sql.append("                 t2.MTIME,                 ");
		sql.append("                 t2.IS_MGR_NODE            ");
		sql.append(" FROM                                      ");
		sql.append(         SI_TABLE("turbo_exch_rel") + " t1, ");
		sql.append(         SI_TABLE("turbo_node_info") + " t2 ");
		sql.append(" LEFT JOIN                                 ");
		sql.append(         SI_TABLE("turbo_sys_info") +  " t3 ");
		sql.append(" ON                                        ");
		sql.append("        t2.id = t3.node_id                 ");
		sql.append(" LEFT JOIN                                 ");
		sql.append(         SI_TABLE("turbo_node_info") + " t4 ");
		sql.append(" ON                                        ");
		sql.append("        t2.PID = t4.ID                     ");
		sql.append(" WHERE                                     ");
		sql.append("     (   t1.node_id_creator = t2.id        ");
		sql.append("      OR t1.node_id_to = t2.id      )      ");
		sql.append(" AND                                       ");
		sql.append("     (   t1.node_id_creator = ?            ");
		sql.append("      OR t1.node_id_to = ?          )      ");
		
		try {
			return CM.getDao().list("通过节点编码获取交换关系节点列表", sql.toString(), new NodeInfoRowWapper(), new Object[] {nodeId,nodeId});
		} catch (DaoException e) {
			throw new DaoException("通过节点编码获取交换关系节点列表失败！", e);
		}
	}
	
	/**
	 * 通过节点编码返回与其相关的适配器列表
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<AdapterInfo> getAllAdapterListByNodeCode(String nodeCode) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT DISTINCT t1.NODE_CODE,                ");
		sql.append("                 t2.NODE_NAME,                ");
		sql.append("                 t1.ADAPTER_CODE,             ");
		sql.append("                 t1.ADAPTER_NAME,             ");
		sql.append("                 t1.USE_SCENE,                ");
		sql.append("                 t1.ADAPTER_TYPE,             ");
		sql.append("                 t1.WSDL_URL,                 ");
		sql.append("                 t1.STATUS,                   ");
		sql.append("                 t1.ISLOCAL,                  ");
		sql.append("                 t1.MTIME,                    ");
		sql.append("                 t1.REMARK                    ");
		sql.append(" FROM                                         ");
		sql.append(         SI_TABLE("turbo_adapter_info") + " t1 ");
		sql.append(" LEFT JOIN                                    ");
		sql.append(         SI_TABLE("turbo_node_info") +    " t2 ");
		sql.append(" ON                                           ");
		sql.append("        t2.NODE_CODE = t1.NODE_CODE           ");
		sql.append(" WHERE                                        ");
		sql.append("        t1.NODE_CODE = ?                      ");
		
		try {
			return CM.getDao().list("通过节点编码返回适配器列表", sql.toString(), new ModuleFeatureBeanRowWapper<AdapterInfo>(AdapterInfo.class), new Object[] {nodeCode});
		} catch (DaoException e) {
			throw new DaoException("通过节点编码返回适配器列表失败！", e);
		}
	}
	
	/**
	 * 通过系统编码返回系统ID
	 * @param sysCode
	 * @return
	 * @throws DaoException
	 */
	public String getSysIdBySysCode(String sysCode) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.ID                             ");
		sql.append(" FROM                                     ");
		sql.append(         SI_TABLE("turbo_sys_info") + " t1 ");
		sql.append(" WHERE                                    ");
		sql.append("        t1.SYS_CODE = ?                   ");
		
		try {
			return CM.getDao().queryForString("通过系统编码返回系统ID", sql.toString(), new Object[] {sysCode});
		} catch (DaoException e) {
			throw new DaoException("通过系统编码返回系统ID失败！", e);
		}
	}
	
	/**
	 * 通过系统ID返回与其相关的适配器列表
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<AdapterInfo> getAllAdapterListBySysId(String sysId) throws DaoException {
		WhereCondition whereCondition = new WhereCondition();
		whereCondition.where().like("t1.SYS_IDS", "%" + sysId + "%");
		
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT DISTINCT t1.NODE_CODE,                   ");
		sql.append("                 t3.NODE_NAME,                   ");
		sql.append("                 t1.ADAPTER_CODE,                ");
		sql.append("                 t1.ADAPTER_NAME,                ");
		sql.append("                 t1.USE_SCENE,                   ");
		sql.append("                 t1.ADAPTER_TYPE,                ");
		sql.append("                 t1.WSDL_URL,                    ");
		sql.append("                 t1.STATUS,                      ");
		sql.append("                 t1.ISLOCAL,                     ");
		sql.append("                 t1.MTIME,                       ");
		sql.append("                 t1.REMARK                       ");
		sql.append(" FROM                                            ");
		sql.append(         SI_TABLE("turbo_adapter_info") + "    t1 ");
		sql.append(" LEFT JOIN                                       ");
		sql.append(         SI_TABLE("turbo_node_info") +       " t3 ");
		sql.append(" ON                                              ");
		sql.append("        t3.NODE_CODE = t1.NODE_CODE              ");
		
		try {
			return CM.getDao().list("通过系统ID返回与其相关的适配器列表", sql.toString(), new ModuleFeatureBeanRowWapper<AdapterInfo>(AdapterInfo.class), whereCondition, new Object[] { });
		} catch (DaoException e) {
			throw new DaoException("通过系统ID返回与其相关的适配器列表失败！", e);
		}
	}
	
	/**
	 * 通过适配器编码返回与其相关的系统列表
	 * @param adapterCode
	 * @return
	 * @throws DaoException
	 */
	public List<AdapterInfo> getSysIdsListByNodeCode(String nodeCode) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.SYS_IDS                               ");
		sql.append(" FROM                                            ");
		sql.append(         SI_TABLE("turbo_adapter_info") +    " t1 ");
		sql.append(" WHERE                                           ");
		sql.append("        t1.NODE_CODE = ?                         ");
		
		try {
			return CM.getDao().listModule("通过适配器编码返回与其相关的系统列表", sql.toString(), AdapterInfo.class, new Object[] {nodeCode});
		} catch (DaoException e) {
			throw new DaoException("通过适配器编码返回与其相关的系统列表失败！", e);
		}
	}
	
	/**
	 * 通过适配器编码返回与其相关的系统列表
	 * @param adapterCode
	 * @return
	 * @throws DaoException
	 */
	public String getSysIdsByAdapterCode(String adapterCode) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.SYS_IDS                               ");
		sql.append(" FROM                                            ");
		sql.append(         SI_TABLE("turbo_adapter_info") +    " t1 ");
		sql.append(" WHERE                                           ");
		sql.append("        t1.ADAPTER_CODE = ?                      ");
		
		try {
			return CM.getDao().queryForString("通过适配器编码返回与其相关的系统ID", sql.toString(), new Object[] {adapterCode});
		} catch (DaoException e) {
			throw new DaoException("通过适配器编码返回与其相关的系统ID失败！", e);
		}
	}
	
	
	
	/**
	 * 获取本地节点
	 * @return
	 * @throws DaoException
	 */
	public NodeInfo getLocalNode() throws DaoException {
		try {
			String netType = NodeInfoParam.NODE_NET_TYPE;
			StringBuffer sql = new StringBuffer("");
			sql.append(" SELECT *                          ");
			sql.append(" FROM                              ");
			sql.append(         SI_TABLE("turbo_node_info") );
			sql.append(" WHERE                             ");
			sql.append("        (    ISLOCAL='1'           ");
			sql.append("         AND NET_TYPE = ?)         ");
			return CM.getDao().get("获取本地节点信息", sql.toString(), new NodeInfoRowWapper(), new Object[] {netType});
		} catch (DaoException e) {
			throw new DaoException("获取本地节点失败！", e);
		}
	}
	
	/**
	 * 根据本地节点ID和父节点编码获取节点信息列表
	 * @param id    :本地节点id
	 * @param pcode :父节点编码
	 * @return
	 * @throws DaoException
	 */
	public List<NodeInfo> getNodeListByPcode(String id,String netType, String pcode) throws DaoException {
		WhereCondition whereCondition = new WhereCondition();
		whereCondition.where().eq("t1.net_type", netType);
		if(!("".equals(pcode))){
			whereCondition.and().eq("t2.NODE_CODE", pcode);
		}
		
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT DISTINCT t1.NODE_CODE,                 ");
		sql.append("        t1.NODE_NAME,                          ");
		sql.append("        t2.NODE_CODE PCODE,                    ");
		sql.append("        t1.STATUS,                             ");
		sql.append("        t1.MTIME,                              ");
		sql.append("        CASE                                   ");
		sql.append("          WHEN t3.node_id_creator is null then ");
		sql.append("          '0'                                  ");
		sql.append("          ELSE                                 ");
		sql.append("          '1'                                  ");
		sql.append("          END ISREL                            ");
		sql.append(" FROM                                          ");
		sql.append(         SI_TABLE("TURBO_NODE_INFO") +     " t1 ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("TURBO_NODE_INFO") +     " t2 ");
		sql.append(" ON                                            ");
		sql.append("        t1.pid = t2.id                         ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("turbo_exch_rel") +      " t3 ");
		sql.append(" ON                                            ");
		sql.append("        (   t1.id = t3.node_id_creator         ");
		sql.append("         OR t1.id = t3.node_id_to     )        ");
		sql.append(" AND                                           ");
		sql.append("        (   t3.node_id_creator = ?             ");
		sql.append("         OR t3.node_id_to = ?     )            ");
		
		try {
			return CM.getDao().list("根据本地节点ID和父节点编码获取节点信息列表", sql.toString(), new NodeInfoRowWapper(), whereCondition, new Object[] {id,id});
		} catch (DaoException e) {
			throw new DaoException("根据本地节点ID和父节点编码获取节点信息列表失败！", e);
		}
	}
	
	public List<AdapterInfo> getAdapterInfoList() throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.NODE_CODE,                         ");
		sql.append("        t1.ADAPTER_CODE,                      ");
		sql.append("        t1.ADAPTER_NAME,                      ");
		sql.append("        t1.USE_SCENE,                         ");
		sql.append("        t1.ADAPTER_TYPE,                      ");
		sql.append("        t1.STATUS,                            ");
		sql.append("        t1.SYS_IDS,                           ");
		sql.append("        t1.MTIME                              ");
		sql.append(" FROM                                         ");
		sql.append(         SI_TABLE("turbo_adapter_info") + " t1 ");
		
		try {
			return CM.getDao().list("返回适配器列表", sql.toString(), new ModuleFeatureBeanRowWapper<AdapterInfo>(AdapterInfo.class), new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("返回适配器列表列表失败！", e);
		}
	}
	
	public String getSysCodeBySysId(String sysId) throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.SYS_CODE                       ");
		sql.append(" FROM                                     ");
		sql.append(         SI_TABLE("turbo_sys_info") + " t1 ");
		sql.append(" WHERE                                    ");
		sql.append("        t1.ID = ?                         ");
		
		try {
			return CM.getDao().queryForString("通过系统ID返回系统编码", sql.toString(), new Object[] {sysId});
		} catch (DaoException e) {
			throw new DaoException("通过系统ID返回系统编码失败！", e);
		}
	}
	
	public List<DataCategory> getDataCategoryList() throws DaoException {
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT t1.DATA_CODE,                          ");
		sql.append("        t1.DATA_NAME,                          ");
		sql.append("        t2.DATA_CODE PCODE,                    ");
		sql.append("        t3.SYS_CODE,                           ");
		sql.append("        t1.STATUS,                             ");
		sql.append("        t1.MTIME                               ");
		sql.append(" FROM                                          ");
		sql.append(         SI_TABLE("turbo_data_category") + " t1 ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("turbo_data_category") + " t2 ");
		sql.append(" ON                                            ");
		sql.append("        t1.PID = t2.ID                         ");
		sql.append(" LEFT JOIN                                     ");
		sql.append(         SI_TABLE("turbo_sys_info") +      " t3 ");
		sql.append(" ON                                            ");
		sql.append("        t1.SYS_ID = t3.ID                      ");
		
		try {
			return CM.getDao().list("返回适配器类型列表", sql.toString(), new  ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class), new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("返回适配器类型列表失败！", e);
		}
	}
	

}
